package com.itheima.date.myexce;

public class ThrowsDemo {
    public static void main(String[] args) {
        /*//method()
                //int [] arr = {1,2,3,4,5};
                int [] arr = null;
                printArr(arr);//就会 接收到一个异常.
                //我们还需要自己处理一下异常.
            }

            private static void printArr(int[] arr) {
                if(arr == null){
                    //调用者知道成功打印了吗?
                    //System.out.println("参数不能为null");
                    throw new NullPointerException(); //当参数为null的时候
                    //手动创建了一个异常对象,抛给了调用者,产生了一个异常
                }else{
                    for (int i = 0; i < arr.length; i++) {
                        System.out.println(arr[i]);
                    }*/
        System.out.println("开始");
        method();
        System.out.println("结束");
    }

    public static void method() {
        try {
            int[] arr = {1, 2, 3};
            System.out.println(arr[3]); //new ArrayIndexOutOfBoundsException();
            System.out.println("这里能够访问到吗");
        } catch (ArrayIndexOutOfBoundsException e) { //new ArrayIndexOutOfBoundsException();
//            e.printStackTrace();

            //public String getMessage():返回此 throwable 的详细消息字符串
            //System.out.println(e.getMessage());
            //Index 3 out of bounds for length 3

            //public String toString():返回此可抛出的简短描述
          //  System.out.println(e.toString());
            //java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

            //public void printStackTrace():把异常的错误信息输出在控制台
            e.printStackTrace();
//            java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
//            at com.itheima_02.ExceptionDemo02.method(ExceptionDemo02.java:18)
//            at com.itheima_02.ExceptionDemo02.main(ExceptionDemo02.java:11)

        }
    }


}


   /* private static void method() {
        try {
            int[] arr = new int[3];
            System.out.println(arr[3]);
            int num = 3 / 0;
            System.out.println("num = " + num);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }catch (AgeOutOfBoundsException e){

        }
    }*/
